package com.jinfang.bankpractice.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jinfang.bankpractice.dto.ReturnData;
import com.jinfang.bankpractice.model.User;
import com.jinfang.bankpractice.service.UserService;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * Created by zl on 2015/8/27.
 */

@RestController
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value="登录",notes="需要用户名，密码")
    @RequestMapping(value="/api/user/login",method=RequestMethod.POST)
    public ReturnData getUserInfo(@RequestParam("username")String username,
    		@RequestParam("password")String password,
    		HttpSession session) {
    	User user=userService.login(username, password);
    	if(user!=null){
    		String sessionid=UUID.randomUUID().toString().replaceAll("-", "");
    		session.setAttribute(sessionid, user);
    		logger.info("{}登录成功！",user.getName());
    		Map<String, String> data=new HashMap<>();
    		data.put("name", user.getName());
    		data.put("sessionid", sessionid);
    		return new ReturnData(200, "登录成功！", "", data);
    	}
    	logger.error("登录失败，username={},password={}",username,password);
    	return new ReturnData(401,"账号或者密码错误","",null);
    }
}

